<template>
  <div>
    <div class="titletopXX">
      <van-nav-bar
        size="30"
        title="黑名单详情"
        left-arrow
        :fixed="true"
        @click-left="onClickLeft"
      />
    </div>
    <van-pull-refresh
      class="pull-refresh"
      v-model="refreshing"
      @refresh="onRefresh"
    >
      <van-empty
        v-if="list.length == 0 && !error"
        image="search"
        description="未找到数据"
      />
      <van-empty v-if="error" image="error" description="网络错误" />
      <van-list
        :immediate-check="false"
        v-model:loading="loading"
        :finished="finished"
        v-model:error="error"
        error-text="请求失败，点击重新加载"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!-- 列表内容 -->
        <div class="ulbox">
          <div
            class="ul"
            v-for="(item, index) in list"
            :key="index"
            @click="TZXQ(item)"
          >
            <div class="lost_title_list">
              <div style="display: flex; justify-content: space-between">
                <div style="width: 70%">
                  <div style="display: flex; align-items: center">
                    <div class="WZ_size_16 marginRight3">
                      {{ item.orderType }}
                    </div>
                    <div class="ZTimg">
                      <van-image
                        v-if="item.orderStatus == 0"
                        class="img"
                        radius="4"
                        width="80"
                        height="25"
                        :src="require('../../../../../../static/SY.png')"
                        mode=""
                      >
                      </van-image>
                      <van-image
                        v-if="item.orderStatus == -1"
                        class="img"
                        radius="4"
                        width="80"
                        height="25"
                        :src="require('../../../../../../static/QX.png')"
                        mode=""
                      >
                      </van-image>
                      <van-image
                        v-if="item.orderStatus == 2"
                        class="img"
                        radius="4"
                        width="80"
                        height="25"
                        :src="require('../../../../../../static/SH.png')"
                        mode=""
                      >
                      </van-image>
                      <van-image
                        v-if="item.orderStatus == 1"
                        class="img"
                        radius="4"
                        width="80"
                        height="25"
                        :src="require('../../../../../../static/WC.png')"
                        mode=""
                      >
                      </van-image>
                      <van-image
                        v-if="item.orderStatus == 3"
                        class="img"
                        radius="4"
                        width="80"
                        height="25"
                        :src="require('../../../../../../static/PJ.png')"
                        mode=""
                      >
                      </van-image>
                      <van-tag
                        class="img BHtag"
                        v-if="item.orderStatus == 4"
                        type="primary"
                        color="#ffe1e1"
                        text-color="#ad0000"
                      >
                        <p class="yuan"></p>
                        已驳回
                      </van-tag>
                    </div>
                  </div>
                  <div class="name WZ_size_14">
                    {{ item.userName }}
                  </div>
                </div>
                <div class="GX">
                  <!-- <van-checkbox
                  v-model="item.checked"
                  @click.stop="GXXX"
                ></van-checkbox> -->
                </div>
              </div>

              <div class="lost_title">
                <van-icon name="clock" size="20" color="#0c7984" />
                <div class="lost_title1 WZ_size_10">时间：</div>
                <div class="XX WZ_size_10">{{ item.creationTime }}</div>
              </div>
              <div class="lost_title">
                <van-icon name="coupon" size="20" color="#0c7984" />
                <div class="lost_title1 WZ_size_10">订单号：</div>
                <div class="XX WZ_size_10">{{ item.orderNumber }}</div>
              </div>
            </div>
          </div>
        </div>

        <template #error>
          <div class="error-tip" @click="retryLoad">
            <van-icon name="warning" />
            <span>加载失败，点击重试</span>
          </div>
        </template>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { Toast, Dialog, Notify } from "vant";
import { getandroidCode } from "../../../../../../common/auth";
import { selectUser } from "@/api/realAssets/icsharedstation/home.js";
import { updateMealBlacklistOrder } from "@/api/realAssets/icsharedstation/CPXX/index.js";
import {
  getServiceOrderList,
  updateServiceOrderUser,
} from "@/api/realAssets/icsharedstation/manager.js";
import { getmoreRoute } from "../../../../../../common/moreRouteList";
import QRCode from "qrcode";
export default {
  data() {
    return {
      mealtodishlist: [],
      list2: [],
      title: "订单列表",
      active: 0,
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      error: false, // 控制错误状态
      CSlist: [],
      XXyz: {
        serviceType: "",
        orderNumber: "",
        mobile: "",
        pageNumber: 1,
        pageSize: 10,
        orderStatus: "0",
        orderType: 1,
        postId: JSON.parse(localStorage.getItem("siteData")).postId,
        siteId: this.$route.query.XX,
      },
    };
  },
  created() {
    if (this.$route.query.XX == "") {
      this.active = "0";
    } else if (this.$route.query.XX == "0") {
      this.active = "1";
    } else if (this.$route.query.XX == "2") {
      this.active = "2";
    } else if (this.$route.query.XX == "-1") {
      this.active = "3";
    }
  },
  mounted() {
    this.getYH();
  },
  methods: {
    clickTab(e) {
      if (e.name == 0) {
        this.XXyz.orderStatus = "";
      } else if (e.name == 1) {
        this.XXyz.orderStatus = "0";
      } else if (e.name == 2) {
        this.XXyz.orderStatus = "2";
      } else if (e.name == 3) {
        this.XXyz.orderStatus = "-1";
      }
      this.onRefresh();
    },
    // 下拉刷新
    onRefresh() {
      //   list.value = [];
      this.XXyz.pageNumber = 1;
      this.XXyz.pageSize = 10;
      this.finished = false;
      this.error = false;
      this.loading = true;
      this.refreshing = true;
      this.onLoad();
    },

    // 点击重试
    retryLoad() {
      this.error = false;
      this.loading = true;
      this.onLoad();
    },
    onLoad() {
      this.loading = true;
      if (this.error) return; // 如果当前是错误状态，不自动触发加载
      if (this.refreshing) {
        this.list = [];
      }

      this.list = JSON.parse(decodeURIComponent(this.$route.query.list));
      this.finished = true;
      this.refreshing = false;
      this.loading = false;
    },
    //默认获取用户信息
    getYH() {
      selectUser({
        ishid: getandroidCode(), //拿到编码
      }).then((resXX) => {
        if (resXX.data != "用户id错误") {
          this.XXyz.mobile = resXX.data.mobile;
          this.onLoad();
        }
      });
    },
    //按键触发
    onSearch() {
      this.onRefresh();
    },
    //清空搜索
    onCancel() {
      // console.log(XXyz.value.districtName);
      this.XXyz.orderNumber = "";
      this.onRefresh();
    },
    //取消订单
    QXDD(val) {
      Dialog.confirm({
        title: "是否取消当前订单",
        message:
          "订单号为：【" + val.orderNumber + "】（取消后请等待管理员审核）",
      })
        .then(() => {
          updateServiceOrderUser({
            id: val.id,
          }).then((res) => {
            if (res.data == "SUCCESS") {
              Toast.success("申请已提交");
              this.onRefresh();
            }
          });
          // on confirm
        })
        .catch(() => {});
    },
    //全选
    GXXX() {},
    //跳转详情页面
    TZXQ(val) {
      let li = encodeURIComponent(JSON.stringify(val));
      if (val.orderType == "疗养登记") {
        // router.push({
        //   name: "i_life_lydj_orderDetail",
        //   query: {
        //     YYXQxx: li,
        //   },
        // });
      } else {
        this.$router.push({
          name: "rvicsharedstation-DD_DDXQ",
          query: {
            XX: li,
          },
        });
      }
    },
    //搜索之前记录信息
    searchXX() {
      this.list2 = [];
      this.list.forEach((item) => {
        if (item.checked) {
          this.list2.push(item);
        }
      });
      this.onRefresh();
    },
    onClickright() {
      this.mealtodishlist = [];
      this.list.forEach((item) => {
        if (item.checked) {
          this.mealtodishlist.push(item);
        }
      });
      if (this.mealtodishlist.length == 0) {
        Dialog.alert({
          title: "操作提示",
          message: "请先勾选订单。",
          closeOnClickOverlay: true,
        }).then(() => {
          // on close
        });
        return;
      }
      // 步骤1：array1中所有checked为true的项都要加入SZ
      const array1CheckedItems = this.list.filter((item) => item.checked);

      // 步骤2：array2中不匹配array1 checked项的项加入SZ
      const array1CheckedIds = new Set(
        array1CheckedItems.map((item) => item.id)
      );
      const array1AllIds = new Set(this.list.map((item) => item.id));

      const unmatchedFromArray2 = this.CSlist.filter(
        (item2) => !array1AllIds.has(item2) // array1中不存在
      ).map((item) => ({
        ...item,
        checked: true,
      }));

      const SZ = [...array1CheckedItems, ...unmatchedFromArray2];

      // console.log(SZ)
      let obj = {
        id: route.query.id,
        serviceOrderList: [],
      };
      SZ.forEach((item) => {
        let objXX = {};
        objXX.orderNumber = item.orderNumber;
        obj.serviceOrderList.push(objXX);
      });
      updateMealBlacklistOrder(obj).then((res) => {
        if (res.resultCode == 1) {
          Notify({ type: "success", message: res.data });
          //   onRefresh();
          this.$router.push({
            name: "i_life_ZGST_HMD",
          });
        } else {
          Notify({ type: "warning", message: "网络错误" });
        }
      });
    },
    onClickLeft() {
      this.$router.push({
        name: "i_life_ZGST_HMD",
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../../../../../static/css/index.less";
@import "../../../../../../static/css/GYcSS.less";
.error-tip {
  padding: 16px;
  color: #f56c6c;
  text-align: center;
}
.error-tip .van-icon {
  margin-right: 4px;
  vertical-align: middle;
}
.lost_title_list {
  width: 100%;
  margin: 0 auto;
  background: #ffffff;
  border-radius: 5px;
  //background-color: #f0f0f0;
  position: relative;
  line-height: 40px;
  margin-bottom: 10px;
  padding: 10px 20px;
  box-sizing: border-box;

  .title {
    font-size: 13px;
  }

  .name {
    color: rgb(158, 158, 158);
  }

  .XX {
    color: rgb(158, 158, 158);
    font-size: 12px;
  }

  // @include respond-to('phone') {
  // 	margin-bottom: 25rpx;
  // 	padding: 20rpx;
  // }

  // @include respond-to('android') {

  // 	padding: 40rpx;
  // }

  .lost_title {
    display: flex !important;
    flex-direction: row;
    // align-content: center;
    align-items: center;
    font-size: 15px;
    // @include respond-to('phone') {
    // 	font-size: 30rpx;
    // }

    // @include respond-to('android') {
    // 	font-size: 80rpx;
    // }

    .lost_title1 {
      font-weight: 700;
      margin-left: 5px;
    }
  }

  .lost-right {
    position: absolute;
    right: 5px;
    bottom: 10px;
    width: 30%;
    height: 100px;

    .lost-btn {
      background: rgba(2, 117, 113, 1);
      width: 50px;
      height: 30px;
      color: #ffffff;
      text-align: center;
      line-height: 20px;
      border-radius: 5px;
      margin-top: 5px;
    }
  }
}
.btn {
  width: 100px;
}
.GX {
  width: 50px;
  position: relative;
  .van-checkbox {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}
.BHtag {
  width: 70px;
  height: 25px;
  box-sizing: border-box;
  overflow: hidden;
  // padding: 3px;
  .yuan {
    width: 5px;
    height: 5px;
    background: #ad0000;
    border-radius: 50%;
    margin: 0px 3px;
    margin-right: 8px;
  }
}
</style>